/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controls;

import Core.Control;
import Core.FGSImage;
import Core.Key;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Александр
 */
public class StringInput extends Control
{
	private FGSImage bg_a = new FGSImage(), bg_p = new FGSImage(), bg_i = new FGSImage();
	private int sx;

	public static Font font = Font.getDefaultFont();
	public static char map[][] = {	{},						// 0
									{'_','~','1'},			// 1
									{'a','b','c','2'},		// 2
									{'d','e','f','3'},		// 3
									{'g','h','i','4'},		// 4
									{'j','k','l','5'},		// 5
									{'m','n','o','6'},		// 6
									{'p','q','r','s','7'},	// 7
									{'t','u','v','8'},		// 8
									{'w','x','y','z','9'}};	// 9

	public String string = "";
	public int active = 0;

	public volatile boolean startNewLetter;
	public static long timeToNewLetter = 1000;
	private Timer activeTimer;

	public StringInput(int width, String def)
	{
		super(width, font.getHeight()+10);

		sx = 0;

		bg_a.load("/ri_a.ngs");
		bg_p.load("/ri_p.ngs");
		bg_i.load("/ri_i.ngs");

		string = def;
		if(def.equals("")) active = 0;
		else active = def.length()-1;

		lockOn = true;
		selectedAction = "Edit";
		pressedAction = "Back";
	}

	public void draw(Graphics g, int x, int y)
	{
		FGSImage bg = null;

		switch(state)
		{
			case Control.ACTIVE:
				bg = bg_a;
				break;
			case Control.INACTIVE:
				bg = bg_i;
				break;
			case Control.PRESSED:
				bg = bg_p;
				break;
		}
		bg.draw(g, x, y, W, H);

		int lx = 0;
		if(string.length() != 0) lx = font.stringWidth(string.substring(0, active));
		int lw;
		if(string.length() != 0)  lw = font.stringWidth(string.charAt(active)+"");
		else lw = font.stringWidth("a");

		x+=5;
		y+=5;

		if(state == Control.PRESSED)
		{
			g.setColor(0xffaa31);
			g.fillRect(lx+x, y, lw, font.getHeight());
		}
		
		g.setColor(0);
		g.drawString(string, x, y, Graphics.LEFT|Graphics.TOP);
	}

	private void add(char c)
	{
		if(string.length() == 0) string += c;
		else if(active == string.length()-1) string += c;
		else
		{
			String left = string.substring(0, active);
			String right = string.substring(active);
			string = left + c + right;
		}
		next();
	}

	private void replace(char c)
	{
		String left = string.substring(0, active);
		String right = string.substring(active+1);
		string = left + c + right;
	}

	private void remove()
	{
		if(string.length() == 0) return;
		String left = string.substring(0, active);
		String right = string.substring(active+1);
		string = left + right;
		prev();
	}

	private void next()
	{
		active ++;
		if(active >= string.length() -1) active = string.length() -1;
	}

	private void prev()
	{
		active --;
		if(active < 0) active = 0;
	}

	public void processKey(int key, int state)
	{
		super.processKey(key, state);

		if(state == Key.PRESSED)
		{
			if(startNewLetter || string.length() == 0)
			{
				if(key >= '1' && key<='9')
				{
					add(map[key-'0'][0]);
					startTimer();
					return;
				}
			}
			else
			{
				if(key >= '1' && key<='9')
				{
					int number=0, position=0;
					boolean found = false;
					for(number=0; number < map.length; number++)
					{
						for(position=0; position < map[number].length; position++)
							if(map[number][position] == string.charAt(active)) 
							{
								found = true;
								break;
							}
						
						if(found) break;
					}

					if(key-'0' == number)
					{
						char next;
						if(position < map[number].length - 1) next = map[number][position+1];
						else next = map[number][0];

						replace(next);
						startTimer();
					}
					else
					{
						add(map[key-'0'][0]);
						startTimer();
					}
				}
			}

			if(key == Key.RS_KEY)
			{
				stopTimer();
				remove();
			}
			if(key == Key.PL_KEY)
			{
				stopTimer();
				prev();
			}
			if(key == Key.PR_KEY)
			{
				stopTimer();
				next();
			}
		}
	}

	private void stopTimer()
	{
		startNewLetter = true;
		if(activeTimer != null) activeTimer.cancel();
	}

	private void startTimer()
	{
		stopTimer();
		startNewLetter = false;
		activeTimer = new Timer();
		activeTimer.schedule(new StringInputTask(this), timeToNewLetter);
	}

	protected class StringInputTask extends TimerTask
	{
		StringInput object;

		public StringInputTask(StringInput object)
		{
			this.object = object;
		}

		public void run()
		{
			if(object != null) object.startNewLetter = true;
		}
	}
}
